/**
 * TODO <br>
 * 0.2: weijj:2019年5月11日 下午5:13:42:TODO <br>
 * @author  weijj
 * @since 2019年5月11日 下午5:13:42
 * @date 2019年5月11日 下午5:13:42
 * @version 0.1 初始为0.1,后续变更则修改
 */
package com.fdb.efp.nls.service.impl.dw;

import com.fdb.basic.framework.core.constant.MonAlrmLvlEnums.MonAlrmLvlEnum;
import com.fdb.basic.framework.core.constant.SDicCertTypeEnum;
import com.fdb.basic.framework.core.util.DateTool;
import com.fdb.efp.nls.service.domain.TxtFileLoadBean;
import com.fdb.efp.nls.service.domain.dw.DwBaiduRepayBean;
import com.fdb.efp.nls.service.facade.dw.DwBaiduRepayBatchService;
import com.fdb.efp.nls.service.util.DueDataUtil;
import com.fdb.efp.nls.service.util.TxtFileLoadPluginBatch;
import com.fdb.efp.nls.service.facade.AccoutErroTempService;
import com.fdb.efp.nls.service.facade.DwBaiduRepayTempService;
import com.fdb.efp.nls.service.facade.LoanRepayDetailService;
import com.fdb.efp.nls.service.vo.AccoutErroTempVO;
import com.fdb.efp.loan.service.vo.LoanRepayDetailVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 根据百度同步过来的贷款每日还款信息临时表数据修改还款明细借据表
 * 
 * @author weijj
 * @since 2019年5月11日 下午5:13:42
 * @version 1.0
 */
@Service("dwBaiduRepayBatchService")
public class DwBaiduRepayBatchServiceImpl implements DwBaiduRepayBatchService {

	private static Logger logger = LoggerFactory.getLogger(DwBaiduRepayBatchServiceImpl.class);

	@Autowired
	@Qualifier("loanRepayDetailService")
	private LoanRepayDetailService loanRepayDetailService;// 还款明细借据信息
	@Autowired
	@Qualifier("dwBaiduRepayTempService")
	private DwBaiduRepayTempService dwBaiduRepayTempService;// 贷款每日还款信息

	@Autowired
	@Qualifier("accoutErroTempService")
	private AccoutErroTempService accoutErroTempService;// 错误信息

	// /**
	// * 根据百度同步过来的贷款每日还款信息临时表数据修改还款明细借据表 <br>
	// * 0.2=1: weijj:2019年5月11日 下午5:29:49:新增 <br>
	// *
	// * @author weijj
	// * @since 2019年5月11日 下午5:29:49
	// * @date 2019年5月11日 下午5:29:49
	// * @version 0.1
	// */
	// @Override
	// public boolean batchUpdateDwBaiduRepay() throws Exception {
	// // TODO Auto-generated method stub
	// boolean result = false;
	// long startTime = System.currentTimeMillis();
	// logger.info("同步过来的贷款每日还款信息临时表数据修改还款明细借据表：[" + DateTool.formatDate(new
	// Date(startTime), DateTool.PATTERN_DATETIME) + "]， 处理开始");
	// // 可以配置参数
	// Integer pageSize = 1000;
	// try {
	// // 单位转换
	// final int oneHundred100 = 100;
	// // 需要新增或者修改的还款明细借据表
	// List<LoanRepayDetailVO> list = null;
	// // 分页信息
	// DwBaiduRepayTempVO pageInfo = new DwBaiduRepayTempVO();
	// // 新增或者修改的还款明细借据表
	// int num = 0;
	// // 页数
	// int page = 1;
	// // pageSize初始化
	// if (pageSize > 0) {
	// pageInfo.setSize(pageSize);
	// } else {
	// pageInfo.setSize(1000);
	// }
	// final String createUser = "admin";
	// // 判断循环次数
	// int size = dwBaiduRepayTempService.queryCount();
	// String size_msg = "【还款明细借据临时表】数据量：" + size;
	// logger.info(size_msg, "message{}");
	// int loopCount = (size / pageInfo.getSize()) + 1;
	// for (int i = 0; i < loopCount; i++) {
	// // 分页查询【还款明细借据表】数据
	// List<DwBaiduRepayTempVO> tempList =
	// dwBaiduRepayTempService.queryByPage(pageInfo);
	// list = new ArrayList<LoanRepayDetailVO>();
	// if (tempList != null && !tempList.isEmpty()) {
	// for (DwBaiduRepayTempVO dwBaiduRepayTempVO : tempList) {
	// // 还款明细借据表
	// LoanRepayDetailVO loanRepayDetailVO = new LoanRepayDetailVO();
	// loanRepayDetailVO.setSetlSeq(dwBaiduRepayTempVO.getSeqNo());
	// loanRepayDetailVO.setLoanNo(dwBaiduRepayTempVO.getLoanId());
	// loanRepayDetailVO.setIdType(SDicCertTypeEnum.IDCARD.getValue());
	// loanRepayDetailVO.setAllOdIntAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getPnltIntAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setAllOdNormIntAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getIntAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setAllOdPrcpAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getPrinAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setTotalAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getTotalAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setSetlOdIntAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getPnltIntAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setSetlOdNormInt(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getIntAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setSetlOdPrcpAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getPrinAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setSetlApplyDt(dateInfoTrans(dwBaiduRepayTempVO.getTranDate()));
	// loanRepayDetailVO.setSetlCreateUsr(createUser);
	// loanRepayDetailVO.setSetlCreateDt(formatDateTime10To19(dwBaiduRepayTempVO.getEtlDate()));
	// loanRepayDetailVO.setLastChgUsr(createUser);
	// loanRepayDetailVO.setLastChgDt(DateTool.getCurrentDateTime());
	// loanRepayDetailVO.setIncomeAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getIncomeAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setIntReducedAmtCoupon(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getIntReducedAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setPnltReducedAmtCoupon(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getPnltReducedAmt(),
	// oneHundred100));
	// loanRepayDetailVO
	// .setFundFeeReducedAmtCoupon(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getFundFeeReducedAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setFundFeeAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getFundFeeAmt(),
	// oneHundred100));
	// loanRepayDetailVO
	// .setChargesReducedAmtCoupon(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getChargesReducedAmt(),
	// oneHundred100));
	// loanRepayDetailVO
	// .setOverdueReducedAmtCoupon(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getOverdueReducedAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setRepayViolateReducedAmtCoupon(
	// DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getRepayViolateReducedAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setRefundViolateReducedAmtCoupon(
	// DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getRefundViolateReducedAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setServiceReducedAmtCoupon(dwBaiduRepayTempVO.getServiceReducedAmt());
	// loanRepayDetailVO.setChargesAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getChargesAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setOverdueAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getOverdueAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setRepayViolateAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getRepayViolateAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setRefundViolateAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayTempVO.getRefundViolateAmt(),
	// oneHundred100));
	// loanRepayDetailVO.setServiceAmt(dwBaiduRepayTempVO.getServiceAmt());
	// list.add(loanRepayDetailVO);
	// }
	// // 需要新增或者更新的还款明细借据表
	// if (list != null && !list.isEmpty()) {
	// int updateSize = loanRepayDetailService.insertOrUpdateLoanRepayDetail(list);
	// num += updateSize;
	// }
	// }
	// pageInfo.setPage(++page);
	// }
	// String num_msg = "【还款明细借据表】插入或更新数据处理结果:" + num;
	// logger.info(num_msg, "message{}");
	// result = true;
	// } catch (Exception e) {
	// e.printStackTrace();
	// String tableName = "loan_repay_detail";
	// String errorMsg = "同步过来的贷款每日还款信息临时表数据修改还款明细借据表出错";
	// String remark = "同步过来的贷款每日还款信息临时表数据修改还款明细借据表出错，错误信息：" + e.getMessage();
	// addErrorInfo(tableName, MonAlrmLvlEnum.SERIOUS.getEnname(), errorMsg,
	// remark);
	// logger.error(remark);
	// throw new Exception(remark);
	// }
	// long endTime = System.currentTimeMillis();
	// logger.info("同步过来的贷款每日还款信息临时表数据修改还款明细借据表：[" + DateTool.formatDate(new
	// Date(endTime), DateTool.PATTERN_DATETIME) + "]， 处理结束");
	// logger.info("SERIOUS，耗时：[" + (endTime - startTime) / 1000.0 + "]秒，处理结果[" +
	// result + "]");
	// return result;
	// }

	/**
	 * 根据百度同步过来的贷款每日还款信息临时表数据修改还款明细借据表 <br>
	 * 0.2=1: weijj:2019年5月11日 下午5:29:49:新增 <br>
	 * 
	 * @author weijj
	 * @since 2019年5月11日 下午5:29:49
	 * @date 2019年5月11日 下午5:29:49
	 * @version 0.1
	 */
	@Override
	public boolean batchUpdateDwBaiduRepay(String fileUrl) throws Exception {
		// 处理结果
		boolean result = false;
		long startTime = System.currentTimeMillis();
		logger.info("同步过来的贷款每日还款信息临时表数据修改还款明细借据表：[" + DateTool.formatDate(new Date(startTime), DateTool.PATTERN_DATETIME) + "]， 处理开始");
		try {
			TxtFileLoadPluginBatch txtFileLoadPlugin = new TxtFileLoadPluginBatch(fileUrl, "\u0002", "UTF-8", 0, 1000);
			List<TxtFileLoadBean> txtFileLoadList = txtFileLoadPlugin.run(new DwBaiduRepayBean());
			result = dueData(txtFileLoadList);
			if (txtFileLoadList.size() != 0) {
				while (!txtFileLoadList.get(txtFileLoadList.size() - 1).isReadedComplete) {
					txtFileLoadPlugin.setFirstRead(false);
					txtFileLoadList = txtFileLoadPlugin.run(new DwBaiduRepayBean());
					if (txtFileLoadList.size() == 0) {
						break;
					}
					result = dueData(txtFileLoadList);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			String tableName = "loan_repay_detail";
			String errorMsg = "同步过来的贷款每日还款信息临时表数据修改还款明细借据表出错";
			String remark = "同步过来的贷款每日还款信息临时表数据修改还款明细借据表出错，错误信息：" + e.getMessage();
			addErrorInfo(tableName, MonAlrmLvlEnum.SERIOUS.getEnname(), errorMsg, remark);
			logger.error(remark);
			throw new Exception(remark);
		}
		long endTime = System.currentTimeMillis();
		logger.info("同步过来的贷款每日还款信息临时表数据修改还款明细借据表：[" + DateTool.formatDate(new Date(endTime), DateTool.PATTERN_DATETIME) + "]， 处理结束");
		logger.info("SERIOUS，耗时：[" + (endTime - startTime) / 1000.0 + "]秒，处理结果[" + result + "]");
		return result;
	}

	/**
	 * 根据百度同步过来的贷款每日还款文件信息数据加工初始化还款明细借据信息 <br>
	 * 0.2=1: weijj:2019年5月11日 下午5:29:49:新增 <br>
	 * 
	 * @author weijj
	 * @since 2019年5月11日 下午5:29:49
	 * @date 2019年5月11日 下午5:29:49
	 * @version 0.1
	 */
	public boolean dueData(List<TxtFileLoadBean> txtFileLoadList) {
		logger.info("同步过来的贷款每日还款信息临时表数据修改还款明细借据表加工处理开始！");
		// 需要新增或者修改的还款明细借据信息
		List<LoanRepayDetailVO> list = new ArrayList<LoanRepayDetailVO>();
		// 处理结果
		boolean result = false;
		// 新增或者修改的数量
		int num = 0;
		// 单位转换
		final int oneHundred100 = 100;
		// 创建、修改用户
		final String createUser = "admin";
		DwBaiduRepayBean dwBaiduRepayBean = new DwBaiduRepayBean();
		try {
			for (int i = 0; i < txtFileLoadList.size(); i++) {
				dwBaiduRepayBean = (DwBaiduRepayBean) txtFileLoadList.get(i);
				// 还款明细借据信息
				LoanRepayDetailVO loanRepayDetailVO = new LoanRepayDetailVO();
				loanRepayDetailVO.setSetlSeq(dwBaiduRepayBean.getSeqNo());
				loanRepayDetailVO.setLoanNo(dwBaiduRepayBean.getLoanId());
				loanRepayDetailVO.setIdType(SDicCertTypeEnum.IDCARD.getValue());
				loanRepayDetailVO.setAllOdIntAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getPnltIntAmt(), oneHundred100));
				loanRepayDetailVO.setAllOdNormIntAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getIntAmt(), oneHundred100));
				loanRepayDetailVO.setAllOdPrcpAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getPrinAmt(), oneHundred100));
				loanRepayDetailVO.setTotalAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getTotalAmt(), oneHundred100));
				loanRepayDetailVO.setSetlOdIntAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getPnltIntAmt(), oneHundred100));
				loanRepayDetailVO.setSetlOdNormInt(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getIntAmt(), oneHundred100));
				loanRepayDetailVO.setSetlOdPrcpAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getPrinAmt(), oneHundred100));
				loanRepayDetailVO.setSetlApplyDt(DueDataUtil.dateInfoTrans(dwBaiduRepayBean.getTranDate()));
				loanRepayDetailVO.setSetlCreateUsr(createUser);
				loanRepayDetailVO.setSetlCreateDt(DueDataUtil.formatDateTime10To19(dwBaiduRepayBean.getEtlDate()));
				loanRepayDetailVO.setLastChgUsr(createUser);
				loanRepayDetailVO.setLastChgDt(DateTool.getCurrentDateTime());
				loanRepayDetailVO.setIncomeAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getIncomeAmt(), oneHundred100));
				loanRepayDetailVO.setIntReducedAmtCoupon(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getIntReducedAmt(), oneHundred100));
				loanRepayDetailVO
						.setPnltReducedAmtCoupon(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getPnltReducedAmt(), oneHundred100));
				loanRepayDetailVO
						.setFundFeeReducedAmtCoupon(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getFundFeeReducedAmt(), oneHundred100));
				loanRepayDetailVO.setFundFeeAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getFundFeeAmt(), oneHundred100));
				loanRepayDetailVO
						.setChargesReducedAmtCoupon(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getChargesReducedAmt(), oneHundred100));
				loanRepayDetailVO
						.setOverdueReducedAmtCoupon(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getOverdueReducedAmt(), oneHundred100));
				loanRepayDetailVO.setRepayViolateReducedAmtCoupon(
						DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getRepayViolateReducedAmt(), oneHundred100));
				loanRepayDetailVO.setRefundViolateReducedAmtCoupon(
						DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getRefundViolateReducedAmt(), oneHundred100));
				loanRepayDetailVO.setServiceReducedAmtCoupon(dwBaiduRepayBean.getServiceReducedAmt());
				loanRepayDetailVO.setChargesAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getChargesAmt(), oneHundred100));
				loanRepayDetailVO.setOverdueAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getOverdueAmt(), oneHundred100));
				loanRepayDetailVO.setRepayViolateAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getRepayViolateAmt(), oneHundred100));
				loanRepayDetailVO.setRefundViolateAmt(DueDataUtil.checkDecimalInfo(dwBaiduRepayBean.getRefundViolateAmt(), oneHundred100));
				loanRepayDetailVO.setServiceAmt(dwBaiduRepayBean.getServiceAmt());
				list.add(loanRepayDetailVO);
			}
			if (list != null && !list.isEmpty()) {
				num = loanRepayDetailService.insertOrUpdateLoanRepayDetail(list);
			}
			String num_msg = "【还款明细借据表】插入或更新数据处理结果:" + num;
			logger.info(num_msg, "message{}");
			if (num > 0) {
				result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			String tableName = "loan_repay_detail";
			String errorMsg = "同步过来的贷款每日还款信息临时表数据修改还款明细借据表出错";
			String remark = "同步过来的贷款每日还款信息临时表数据修改还款明细借据表出错，错误信息：" + e.getMessage();
			addErrorInfo(tableName, MonAlrmLvlEnum.SERIOUS.getEnname(), errorMsg, remark);
			logger.error(remark);
		} finally {
			// 清空信息
			list = null;
			txtFileLoadList = null;
		}
		logger.info("同步过来的贷款每日还款信息临时表数据修改还款明细借据表加工处理结束！");
		return result;
	}

	/**
	 * 
	 * 记录异常信息 <br>
	 * 0.1:weijj:2019年5月7日 上午11:31:23:新增 <br>
	 * 
	 * @author weijj
	 * @since 2019年5月7日 上午11:31:23
	 * @date 2019年5月7日 上午11:31:23
	 * @version 0.1
	 */
	private void addErrorInfo(String tableName, String errorType, String errorMsg, String remark) {
		AccoutErroTempVO accoutErroTempVO = new AccoutErroTempVO();
		// 表名
		accoutErroTempVO.setTableName(tableName);
		// 错误级别
		accoutErroTempVO.setErroType(errorType);
		// 错误信息
		accoutErroTempVO.setErroMsg(errorMsg);
		// 备注
		accoutErroTempVO.setRemark(remark);
		// 创建时间
		accoutErroTempVO.setCreateData(DateTool.getCurrentDateTime());
		// 修改时间
		accoutErroTempVO.setLastModifyTime(DateTool.getCurrentDateTime());
		// 记录异常信息
		accoutErroTempService.insert(accoutErroTempVO);
	}
}
